﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate;
using Bizbase.Domain.Mapping;
using Bizbase.Domain.Security;

namespace Bizbase.Repository.Nhibernate
{
    public class Database : IDatabase
    {
        private DatabaseBuilder _databaseBuilder;

        public DatabaseBuilder DatabaseBuilder
        {
            get { return _databaseBuilder; }
            set { _databaseBuilder = value; }
        }

        public Database()
        {
            _databaseBuilder = new DatabaseBuilder(DatabaseType.SqlServer, "Data Source=(local);Integrated Security=True;Initial Catalog=mytest;");

            AddMapping();
        }

        public void BuildDatabase()
        {
            _databaseBuilder.BuildSchema();
        }

        private void AddMapping()
        {
            //_databaseBuilder.AddMappings(typeof(ClassMapping<>).Assembly.GetExportedTypes());
            _databaseBuilder.AddMapping<UserMapping>();
            _databaseBuilder.AddMapping<RoleMapping>();
        }

        public ISessionFactory SessionFactory
        {
            get
            {
                return _databaseBuilder.SessionFactory;
            }
        }
    }
}
